//
//  PageContentView.swift
//  斗鱼直播Swift
//
//  Created by admin on 2018/2/24.
//  Copyright © 2018年 admin. All rights reserved.
//

import UIKit
protocol PageContentViewDelegate : class {
    
    func pageContentView(contentcView:PageContentView,progress:CGFloat,sourceIndex:Int,targetIndex:Int)
}

class PageContentView: UIView ,UICollectionViewDataSource{
    private var childVCs : [UIViewController]
    private var parentVC : UIViewController
    private var startOffset : CGFloat = 0
    weak var delegate : PageContentViewDelegate?

    
    var index : Int = 0 {
        
        didSet {
            let offset = CGFloat (index) * collectionView.frame.width
            collectionView.setContentOffset(CGPoint.init(x: offset, y: 0), animated: false)
        }
    }
    
    private lazy var collectionView : UICollectionView = {
       let layout = UICollectionViewFlowLayout.init()
        layout.itemSize = bounds.size
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .horizontal
    
        let coll = UICollectionView.init(frame: bounds, collectionViewLayout: layout)
        coll.isPagingEnabled = true
        coll.bounces = false
        coll.showsHorizontalScrollIndicator = false
        coll.dataSource = self
        coll.delegate = self
        coll.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "\(UICollectionViewCell.self)")
        return coll
        
    }()
    

    init(frame: CGRect,childVCs:[UIViewController],parentVC:UIViewController) {
        self.childVCs = childVCs
        self.parentVC = parentVC
        super.init(frame: frame)
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
extension PageContentView {
    //添加子控制器
    private func setupUI () {
        for childVC in childVCs {
         parentVC.addChildViewController(childVC)
        }
        collectionView.backgroundColor = UIColor.white
        collectionView.frame = bounds
        addSubview(collectionView)
        collectionView.reloadData()
    }
}

extension PageContentView {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return childVCs.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "\(UICollectionViewCell.self)", for: indexPath)
        for view in cell.contentView.subviews {
            view.removeFromSuperview()
        }
        let childvc = childVCs[indexPath.item]
        childvc.view.frame = cell.contentView.frame
        cell.contentView.addSubview(childvc.view)
        
        return cell
    }
}

extension PageContentView:UICollectionViewDelegate {
    //将要开始拖拽
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
       startOffset = scrollView.contentOffset.x
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        var progress : CGFloat = 0.0 //文字变化比例
        var sourceIndex : Int = 0
        var targetIndex : Int = 0
        let offsetX = scrollView.contentOffset.x
        if offsetX > startOffset { //左滑
            progress = offsetX/scrollView.frame.width - floor(offsetX/scrollView.frame.width)
            sourceIndex = Int(offsetX/scrollView.frame.width)
            if targetIndex >= childVCs.count {
                targetIndex = childVCs.count - 1
            }
            //划过一整块屏幕
            if offsetX - startOffset == scrollView.frame.width {
                progress = 1
                sourceIndex = targetIndex
            }
        } else { //右滑
            progress = 1 - (offsetX/scrollView.frame.width - floor(offsetX/scrollView.frame.width))
            targetIndex = Int(offsetX/scrollView.frame.width)
            sourceIndex = targetIndex + 1
            if sourceIndex >= childVCs.count {
                sourceIndex = childVCs.count - 1
            }
        }
        
        delegate?.pageContentView(contentcView: self, progress: progress, sourceIndex: sourceIndex, targetIndex: targetIndex)
    }
}

extension PageContentView {
    func setCurrentIndex(currentIndex:Int) -> Void {
        let offset = CGFloat (currentIndex) * collectionView.frame.width
        collectionView.setContentOffset(CGPoint.init(x: offset, y: 0), animated: false)
        
    }
}
